package com.yanqu.road.server.manger.activity.summergames;

import com.yanqu.road.dao.impl.activity.summergames.SummerGamesConfigDaoImpl;
import com.yanqu.road.dao.impl.activity.summergames.SummerGamesUserDataDaoImpl;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.summergames.SummerGamesConfig;
import com.yanqu.road.entity.activity.summergames.SummerGamesSelectReward;
import com.yanqu.road.entity.activity.summergames.SummerGamesUserData;
import com.yanqu.road.entity.activity.summergames.config.SummerGamesLotteryInfo;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.logic.helper.RandomWeightConfigHelper;
import com.yanqu.road.pb.activity.SummerGamesProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.summergames.SummerGamesModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.pb.PlayerPb;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Random;

public class SummerGamesActivityMgr extends TempMgr {

    private static SummerGamesConfig summerGamesConfig;

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static void reloadActivityData() {
        getLogger().info("reload SummerGames Activity start");
        ActivityInfo tempActivityInfo;
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.SummerGames.getValue());
        if (openActivityInfoList.isEmpty()) {
            getLogger().info("no SummerGames Activity in show time");
            summerGamesConfig = null;
        } else {

            tempActivityInfo = openActivityInfoList.get(0);
            int tmpActivityId = tempActivityInfo.getActivityId();

            SummerGamesConfig configTemp = new SummerGamesConfig(tempActivityInfo);

            //config配置
            Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(tmpActivityId);

            SummerGamesConfigDaoImpl dao = new SummerGamesConfigDaoImpl();
//
            configTemp.setLotteryInfoList(dao.getSummerGamesLotteryInfoList(tmpActivityId));
            configTemp.setProjectInfoList(dao.getSummerGamesProjectInfoList(tmpActivityId));
            configTemp.setConvertInfoList(dao.getSummerGamesConvertInfoList(tmpActivityId));
//
            configTemp.setItemId(activityConfigMap.get(eActivityConfigType.SUMMERGAME_ITEM_ID.getName()).getIntValue());
            configTemp.setEXCHANGE_SCORE_ITEM_ID(activityConfigMap.get(eActivityConfigType.EXCHANGE_SCORE_ITEM_ID.getName()).getIntValue());
            configTemp.setTenGachaLimit(activityConfigMap.get(eActivityConfigType.SUMMERGAME_TEN_GACHA_LIMIT.getName()).getIntValue());

            configTemp.setSUMMERGAME_ITEM_USE_REWARD(activityConfigMap.get(eActivityConfigType.SUMMERGAME_ITEM_USE_REWARD.getName()).getValue());
            configTemp.setSUMMERGAME_EXCHANGE_NUM_LIMIT(activityConfigMap.get(eActivityConfigType.SUMMERGAME_EXCHANGE_NUM_LIMIT.getName()).getIntValue());
            configTemp.setSUMMERGAME_MEDALS_RECLAIM(activityConfigMap.get(eActivityConfigType.SUMMERGAME_MEDALS_RECLAIM.getName()).getValue());

            configTemp.setFixNum(activityConfigMap.get(eActivityConfigType.SUMMERGAME_LOTTERY_PARAM_1.getName()).getIntValue());

            String param2 = activityConfigMap.get(eActivityConfigType.SUMMERGAME_LOTTERY_PARAM_2.getName()).getValue();
            if(!StringUtils.isNullOrEmpty(param2)){
                List<String> stringList = StringUtils.stringToStringList(param2, "\\|");
                for (String param : stringList) {
                    List<Integer> valList = StringUtils.stringToIntegerList(param, ";");
                    if(valList.size() != 4){
                        getLogger().error("固定保底配置错误 {}", param2);
                        return;
                    }
                    SummerGamesLotteryInfo lotteryInfo = configTemp.getSummerGamesLotteryInfo(valList.get(0));
                    lotteryInfo.setFixedWeight(valList.get(1));
                    if(valList.get(2) > 0){
                        lotteryInfo.getFixedNumMap().add(valList.get(2));
                    }
                    if(valList.get(3) > 0){
                        lotteryInfo.getFixedNumMap().add(valList.get(3));
                    }
                }
            }

            //检查配置
            RandomWeightConfigHelper.getProperty(configTemp.getSUMMERGAME_ITEM_USE_REWARD(), new Random());
            summerGamesConfig = configTemp;
        }

        getLogger().info("reload SummerGames Activity finish");

        // 在线玩家下发数据
        for (GamePlayer gamePlayer : GamePlayerMgr.getAllOnlinePlayer()) {
            SummerGamesModule module = gamePlayer.getModule(SummerGamesModule.class);
            module.activityReload();
        }
    }

    public static SummerGamesProto.SummerGamesSearchUserRespMsg.Builder searchUser(int activityId, long userId){
        SummerGamesProto.SummerGamesSearchUserRespMsg.Builder builder = SummerGamesProto.SummerGamesSearchUserRespMsg.newBuilder();
        builder.setRet(0);
        builder.setScore(0);
        UserInfo userInfo = UserMgr.getUserInfoInRAM(userId);
        if(userInfo == null){
            builder.setRet(GameErrorCode.E_PLAYER_NO_FOUND);
            return builder;
        }

        SummerGamesConfig config = SummerGamesActivityMgr.getSummerGamesConfig();
        if(config != null && config.getActivityId() == activityId){
            GamePlayer onlinePlayer = GamePlayerMgr.getOnlinePlayer(userId);
            builder.setUserData(PlayerPb.parsePlayerBaseInfoTemp(userInfo));
            if(onlinePlayer != null){
                SummerGamesUserData userData = onlinePlayer.getModule(SummerGamesModule.class).getUserData();
                if(userData != null){
                    builder.setScore(userData.getScore());
                }
            }else {
                SummerGamesUserData userData = new SummerGamesUserDataDaoImpl().getSummerGamesUserData(activityId, userId);
                if(userData != null){
                    builder.setScore(userData.getScore());
                }
            }
        }else {
            builder.setRet(GameErrorCode.E_REQUEST_ARGUMENT_ERROR);
        }
        return builder;
    }

    public static SummerGamesConfig getSummerGamesConfig() {
        return summerGamesConfig;
    }

    public static ActivityInfo getActivityInfo(){
        if(summerGamesConfig == null){
            return null;
        }
        return summerGamesConfig.getActivityInfo();
    }

    public static SummerGamesProto.SummerGamesSelectRewardTemp.Builder buildSummerGamesSelectRewardTemp(SummerGamesSelectReward selectReward){
        SummerGamesProto.SummerGamesSelectRewardTemp.Builder builder = SummerGamesProto.SummerGamesSelectRewardTemp.newBuilder();
        builder.setId(selectReward.getId());
        builder.setSelect(selectReward.getSelect());
        for (Map.Entry<String, Integer> entry : selectReward.getSelectMap().entrySet()) {
            SummerGamesProto.SummerGamesSelectRewardTimesTemp.Builder timeBuilder = SummerGamesProto.SummerGamesSelectRewardTimesTemp.newBuilder();
            timeBuilder.setSelect(entry.getKey());
            timeBuilder.setTimes(entry.getValue());
            builder.addTimes(timeBuilder);
        }
        return builder;
    }
}
